package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.enums.UserType;
import com.weipai.springboot.model.Artisan;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.DemandCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.ArtisanDao;
import com.weipai.springboot.module.dao.CustomerDao;
import com.weipai.springboot.module.mapper.ArtisanMapper;
import com.weipai.springboot.module.mapper.DemandCategoryMapper;
import com.weipai.springboot.module.service.ArtisanService;
import com.weipai.springboot.module.util.DateUtils;
import com.weipai.springboot.module.util.SessionUserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/09 16:22
 * <p>
 * 技工
 */
@Service
public class ArtisanServiceImpl implements ArtisanService {

    private static final Logger log = LoggerFactory.getLogger(ArtisanServiceImpl.class);

    @Autowired
    ArtisanDao artisanDao;
    @Autowired
    SessionUserUtil sessionUserUtil;
    @Autowired
    private CustomerDao customerDao;
    @Autowired
    ArtisanMapper artisanMapper;
    @Autowired
    DemandCategoryMapper demandCategoryMapper;


    //保存
    @Override
    public Response save(Artisan artisan) {
        Response response = new Response();
        Artisan artisan1 = artisanDao.getArtisanByMobile(artisan.getArtisanMobile());
        if(null!=artisan1){
            response.setCode(RetCode.ARTISAN_MOBILE);
            return response;
        }
        String customerId = sessionUserUtil.getUserAttr().getCustomerId();
        //员工不可以申请为技工
        Customer customer = customerDao.getCustomerById(customerId);
        if(customer.getCustomerCategoryId()==UserType.EMPLOYEE.getType()){
            response.setCode(RetCode.REQUEST_ERROR_ARTISAN);
            return response;
        }
        //大客户不可以申请为技工
        if(customer.getCustomerCategoryId()==UserType.CUSTOMER.getType()){
            response.setCode(RetCode.REQUEST_ERROR_CUSTOMER);
            return response;
        }
        //判断此用户是否注册过技工,
        Artisan artisan2  =artisanDao.getArtisanByCustomerId(customerId);
        if (artisan2!=null){
            response.setCode(RetCode.ARTISAN_VER);
            return response;
        }else{
            artisan.setCustomerId(customerId);
            if (1 == artisanDao.insert(artisan)) {
                response.setCode(RetCode.SUCCESS);
            } else {
                log.debug("添加技工失败:{}", artisan.getArtisanName());
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }
        return response; 
    }

    //分页查询
    @Override
    public List<Map<String, Object>> selectAllByDel(int del, String search, String sort, String way, String isAudit,String demandCategoryId) {
        return artisanDao.selectAllByDel(del, search, sort, way, isAudit,demandCategoryId);
    }

    //技工的详情
    @Override
    public Response getArtisanById(String id) {
        Response response = new Response();
        Artisan artisan = artisanDao.getArtisanById(id);
        if (null != artisan) {
            response.setCode(RetCode.SUCCESS);
            response.setData(artisan);
        } else {
            response.setData(RetCode.NO_DATA);
        }
        return response;
    }

    //通过customerId    ----------查询技工详情---------技工申请页面
    @Override
    public Response getArtisanByCustomerId() {
        Response response = new Response();
        String customerId = sessionUserUtil.getUserAttr().getCustomerId();
        Artisan artisan = artisanDao.getArtisanByCustomerId(customerId);
        if (null != artisan) {
           DemandCategory demandCategory = demandCategoryMapper.getDemandCategoryById(artisan.getDemandCategoryId());
            artisan.setDelTime(demandCategory.getDemandCategoryName());  //返回  需求类别 汉字说明 , 暂用 这个字段
            response.put("artisan",artisan);
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response getArtisanByCustomer(String customerId) {
        Response response = new Response();
        Artisan artisan = artisanMapper.getArtisanByCustomerId(customerId);
        if(artisan!=null){
            response.setCode(RetCode.SUCCESS);
            response.setData(artisan);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }


    //删除
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response deleteById(String id) {
        Response response = new Response();
        
        Artisan artisan = artisanDao.getArtisanById(id);
        customerDao.changeCustomerCategory(artisan.getCustomerId(), "1");
        if (artisanDao.updateDel(id) == 1) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.DEL_ERROR);
        }
        return response;
    }

    //批量删除
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        for(int i=0;i<id.length;i++){
            artisanDao.updateDel(id[i]);
            Artisan artisan = artisanDao.getArtisanById(id[i]);
            customerDao.changeCustomerCategory(artisan.getCustomerId(), "1");
        }
        response.setCode(RetCode.SUCCESS);
        /*if (artisanDao.batchDeleteById(id) > 0) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.DEL_ERROR);
        }*/
        return response;
    }

    //技工的删除
    @Override
    public Response changeStatus(String id, Integer status) {
        Response response = new Response();
        if (1 == artisanDao.updateStatus(id, status)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //修改技工的信息
    @Override
    public Response update(Artisan artisan) {
        Response response = new Response();

        //如果信息被驳回, 重新申请技工 , 更新 技工信息
        if(artisan.getIsAudit()==-1){
            artisan.setIsAudit(0);
                if(artisan.getDemandCategoryId().equals("")){
                    artisan.setDemandCategoryId(null);
                }
            }
        artisan.setUpdateTime(DateUtils.timeFormat(System.currentTimeMillis()));
        if (1 == artisanDao.updateByPrimaryKey(artisan)) {
            response.setCode(RetCode.SUCCESS);
        } else {
            log.debug("修改技工信息失败:{}", artisan.getArtisanName());
            response.setCode(RetCode.SAVE_ERROR);
        }
        return response;
    }

    //首页推荐技工
    @Override
    public Response recommendArtisan(String city) {
        Response response = new Response();
        List<Map<String, Object>> artisans = artisanDao.recommendArtisan(city);
        if (null != artisans && artisans.size() > 0) {
            response.setCode(RetCode.SUCCESS);
            response.put("artisanList", artisans);
        } else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    @Override
    public Response changeIsAudit(String id, Integer isAudit) {
        Response response = new Response();
        if (1 == artisanDao.changeIsAudit(id, isAudit)) {
            if(1==isAudit){
                Artisan artisan = artisanDao.getArtisanById(id);
                if(1==customerDao.changeCustomerCategory(artisan.getCustomerId(), String.valueOf(UserType.ARTISAN.getType()))){
                    response.setCode(RetCode.SUCCESS);
                }else{
                    response.setCode(RetCode.REQUEST_ERROR);
                }
            }else{
                response.setCode(RetCode.SUCCESS);
            }
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }


}
